package com.example.cloud.controller.business.reservation;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.business.reservation.TreatmentTask;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.service.business.reservation.TreatmentTaskService;
import com.example.cloud.utils.DateUtils;
import com.example.cloud.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/treatment/task")
public class TreatmentTaskController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(TreatmentTaskController.class);

    @Autowired
    private TreatmentTaskService treatmentTaskService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, TreatmentTask.class);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TreatmentTask record){
        try {
            record.setStartTime(DateUtils.parseDate(record.getStartTimeStr(), CommonConstants.DATE_FORMATS));
            record.setEndTime(DateUtils.parseDate(record.getEndTimeStr(), CommonConstants.DATE_FORMATS));
        } catch (ParseException e) {
            logger.error("日期转换异常:", e);
            return ResultUtil.fail("日期格式不正确");
        }
        return treatmentTaskService.doSave(record);
    }

    @PostMapping("/cancel")
    public void cancel(@RequestBody List<Long> ids){
        treatmentTaskService.doCancel(ids);
    }

    @PostMapping("/admission")
    public void admission(@RequestBody TreatmentTask record){
        treatmentTaskService.doAdmission(record);
    }

    @PostMapping("/complete")
    public void complete(@RequestBody TreatmentTask record){
        treatmentTaskService.doComplete(record);
    }
}
